#! /usr/bin/env python3
# coding: utf-8
# Filename: text2BlogData.py
import sys
class floor:
    '''层类.

       数据:级数,开始行号,结束行号.
       方法:添加标签.'''
    def __init__(self,level,start):
        self.level = level
        self.start = start
        self.end = start
class row:
    '''行类.

       数据:行号,缩进数,等级
       方法:计算级别'''
    def __init__(self,numOfSpace):
        self.numOfSpace = numOfSpace
        self.level = -1
    def doLevel(self,levelTable):
        i = 0
        for numOfSpace in levelTable:
            if self.numOfSpace == numOfSpace:
                break
            else: i += 1
        assert i < len(levelTable)
        self.level = i
def hasChild(aFloor,rows):
    '''判断当前层有没有子层'''
    rowIndex = aFloor.start
    if rowIndex + 1 < len(rows):
        if rowIndex + 1 < len(rows) and rows[rowIndex + 1].level > aFloor.level:
            return True
    return False
def addTag(tag,aFloor,allLines):
    '''用标签将层包住'''
    closeOfTag = '</'+tag[1:]
    allLines[aFloor.start] = tag + allLines[aFloor.start]
    allLines[aFloor.end] = allLines[aFloor.end] + closeOfTag
def hasNum(num,level):
    '''查询当前缩进数在等级表中有没有'''
    for l in level:
        if l == num:
            return True
    return False
def getNumOfSpace(line):
    '''得到当前行的缩进数'''
    numOfSpace = 0
    for char in line:
       if char == ' ': numOfSpace = numOfSpace + 1
       else: return numOfSpace
def checkRow(rowIndex,rows,level,isFinded,floorList):
    '''递归设置当前level的层表'''
    if rowIndex >= len(rows):
        floorList[-1].end = rowIndex - 1
        return
    aRow = rows[rowIndex]
    if not isFinded:
        if aRow.level == level:
            newFloor = floor(level,rowIndex)
            floorList.append(newFloor)
            checkRow(rowIndex + 1,rows,level,True,floorList)
        else:
            checkRow(rowIndex + 1,rows,level,False,floorList)
    else:
        if aRow.level <= level:
            floorList[-1].end = rowIndex - 1
            checkRow(rowIndex,rows,level,False,floorList)
        else:
            checkRow(rowIndex + 1,rows,level,True,floorList)
